package easy;

public class Solution_371 {
    public static int getSum(int a, int b) {
        if (a<0 && b<0) {
            int m = a<0? getSum(~a, 1):a;//a的绝对值
            int n = b<0? getSum(~b, 1):b;//b的绝对值
            return getSum(~reverse(m, n), 1);
        } else if ((a<0 && b>=0) || (a>=0 && b<0)) {
            return reverse(a, b);
        } else {
            return reverse(a, b);
        }

        /*boolean flag = false;//判断结果的符号
        if ((a>=0 && b>=0) || (a>=0 && a>=n) || (b>=0 && b>=m)) {
            flag = true; // a、b同时为负数
        }
        if (a>=0 && b>=0) {
            return reverse(a, b);
        } else if(a<0 && b<0){
            return reverse(~reverse(m, n), 1);
        } else {
            int c = m^n;
            return flag?c:reverse(~c, 1);
        }*/
    }

    private static int reverse(int a,int b) {
        int c = a^b;
        int d = a&b;
        while (d!=0) {
            d = d<<1;
            int e = c&d;
            c = c^d;
            d = e;
        }
        return c;
    }

    public static void main(String[] args) {
//        System.out.println(reverse(3,-5));
//        System.out.println(reverse(-5,3));
        System.out.println(getSum(0,0));
        System.out.println(getSum(-1,0));
        System.out.println(getSum(5,0));
        System.out.println(getSum(-5,0));
        System.out.println(getSum(3,-3));
        System.out.println(getSum(-3,3));
        System.out.println(getSum(1,2));
        System.out.println(getSum(2,5));
        System.out.println(getSum(89,32));
        System.out.println(getSum(-2,3));
        System.out.println(getSum(3,-2));
        System.out.println(getSum(3,-3));
        System.out.println(getSum(-23,-32));
        System.out.println(getSum(-12,-8));
        System.out.println(getSum(-14,15));
        System.out.println(getSum(-14,-16));
//        System.out.println(3^2);
//        System.out.println(2^3);
//        System.out.println(3^3);
//        System.out.println(reverse(~5, 1));
    }
}
